<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Dom的增删改查</title>
    <style>
      .container {
        width: 80%;
        margin: 10px auto;
      }
      .left {
        display: inline-block;
        border: 1px solid #000;
        padding: 10px;
        height: fit-content;
        width: 40%;
      }
      #city {
        margin: 0;
        list-style: none;
        padding: 0;
        display: flex;
      }
      #city li {
        background-color: green;
        margin-right: 10px;
        padding: 5px;
        border-radius: 2px;
      }
      .right {
        margin-left: 20px;
        display: inline-flex;
        flex-direction: column;
        vertical-align: top;
        width: 50%;
      }
      button {
        margin-bottom: 5px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="left">
        <p>你最喜欢哪个城市？</p>
        <ul id="city">
          <li id="bj">北京</li>
          <li>上海</li>
          <li>京东</li>
          <li>首尔</li>
        </ul>
      </div>
      <div class="right">
        <button id="btn1">创建一个“广州”节点，添加到#city下</button>
        <button id="btn2">将“广州”节点插入到#bj前面</button>
        <button id="btn3">使用“广州”节点替换#bj节点</button>
        <button id="btn4">删除#bj节点</button>
        <button id="btn5">读取#city内的HTML代码</button>
        <button id="btn6">设置#bj内的HTML代码</button>
      </div>
    </div>
    <script>
      function onClick(idStr, callback) {
        var element = document.getElementById(idStr);
        element.onclick = callback;
      }

      // 创建一个“广州”节点，添加到#city下
      onClick("btn1", function () {
        var city = document.getElementById("city");

        var li = document.createElement("li");
        /* var text = document.createTextNode("广州");
        li.appendChild(text); */
        li.innerHTML = "广州";

        city.appendChild(li);
      });

      // 将“广州”节点插入到#bj前面
      onClick("btn2", function () {
        var li = document.createElement("li");
        var text = document.createTextNode("广州");
        li.appendChild(text);

        var bj = document.getElementById("bj");
        var city = document.getElementById("city");

        city.insertBefore(li, bj);
      });

      // 使用“广州”节点替换#bj节点
      onClick("btn3", function () {
        var li = document.createElement("li");
        var text = document.createTextNode("广州");
        li.appendChild(text);

        var bj = document.getElementById("bj");
        var city = document.getElementById("city");

        city.replaceChild(li, bj);
      });

      // 删除#bj节点
      onClick("btn4", function () {
        var bj = document.getElementById("bj");
        var parent = bj.parentNode;
        parent.removeChild(bj);
      });

      // 读取#city内的HTML代码
      onClick("btn5", function () {
        var city = document.getElementById("city");
        console.log(city.innerHTML);
      });

      // 设置#bj内的HTML代码
      onClick("btn6", function () {
        var bj = document.getElementById("bj");
        bj.innerHTML = "替换后的北京";
      });
    </script>
  </body>
</html>
